From: Yehuda Katz Date: Thu, 19 Jun 2014 18:49:50 +0000 (-0700) Subject: Add CargoError::with_cause X-Git-Tag: archive/raspbian/0.35.0-2+rpi1~3^2^2^2^2^2^2^2~996 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/success//%22http:/www.example.com/cgi/success/?a=commitdiff_plain;h=90149584c0213cc7cb85690dad711ecd03168ecf;p=cargo.git Add CargoError::with_cause This allows concrete implementations with a `cause` field to have a better implementation than converting the entire original error into a concrete representation. --- diff --git a/src/cargo/util/errors.rs b/src/cargo/util/errors.rs index dc5367550..f23a329a4 100644 --- a/src/cargo/util/errors.rs +++ b/src/cargo/util/errors.rs @@ -28,6 +28,12 @@ pub trait CargoError { } } + fn with_cause(self, cause: E) -> Box { + let mut concrete = self.concrete(); + concrete.cause = Some(cause.box_error()); + box concrete as Box + } + fn mark_human(self) -> Box { let mut concrete = self.concrete(); concrete.is_human = true; @@ -103,9 +109,7 @@ impl BoxError for Result { impl ChainError for Result { fn chain_error(self, callback: || -> E) -> CargoResult { self.map_err(|err| { - let mut update = callback().concrete(); - update.cause = Some(err.box_error()); - box update as Box + callback().with_cause(err) }) } } @@ -159,6 +163,11 @@ impl CargoError for ProcessError { fn cause<'a>(&'a self) -> Option<&'a CargoError> { self.cause.as_ref().map(|c| { let err: &CargoError = *c; err }) } + + fn with_cause(mut self, err: E) -> Box { + self.cause = Some(err.box_error()); + box self as Box + } } pub struct ConcreteCargoError { @@ -187,6 +196,11 @@ impl CargoError for ConcreteCargoError { self.cause.as_ref().map(|c| { let err: &CargoError = *c; err }) } + fn with_cause(mut self, err: E) -> Box { + self.cause = Some(err.box_error()); + box self as Box + } + fn is_human(&self) -> bool { self.is_human } @@ -215,7 +229,7 @@ impl CliError { let error = if error.is_human() { error } else { - chain(error, human("An unknown error occurred")) + human("An unknown error occurred").with_cause(error) }; CliError { error: error, exit_code: code } @@ -259,9 +273,3 @@ pub fn human(error: S) -> Box { is_human: true } as Box } - -pub fn chain(original: Box, update: E) -> Box { - let mut concrete = update.concrete(); - concrete.cause = Some(original); - box concrete as Box -} diff --git a/src/cargo/util/mod.rs b/src/cargo/util/mod.rs index e88e9bfdc..918f90a41 100644 --- a/src/cargo/util/mod.rs +++ b/src/cargo/util/mod.rs @@ -2,7 +2,7 @@ pub use self::config::Config; pub use self::process_builder::{process,ProcessBuilder}; pub use self::result::{Wrap, Require}; pub use self::errors::{CargoResult, CargoError, BoxError, ChainError, CliResult, CliError, ProcessError}; -pub use self::errors::{process_error, internal_error, internal, human, chain}; +pub use self::errors::{process_error, internal_error, internal, human}; pub use self::paths::realpath; pub mod graph; diff --git a/src/cargo/util/result.rs b/src/cargo/util/result.rs index 8e0b7b563..d47ca51e8 100644 --- a/src/cargo/util/result.rs +++ b/src/cargo/util/result.rs @@ -1,4 +1,4 @@ -use util::errors::{CargoResult, CargoError, chain}; +use util::errors::{CargoResult, CargoError}; pub trait Wrap { fn wrap(self, error: E) -> Self; @@ -8,7 +8,7 @@ impl Wrap for Result> { fn wrap(self, error: E) -> CargoResult { match self { Ok(x) => Ok(x), - Err(e) => Err(chain(e, error)) + Err(e) => Err(error.with_cause(e)) } } }